#ifndef QXCCOMMUNICATIONS_H_
#define QXCCOMMUNICATIONS_H_
/************************************************************************************
*	Copyright Paxs Electronics 2012													*
*   All Rights Reserved																*
*   The Copyright symbol does not also indicate public availability or publication.	*
* 																					*
* 								PAXS ELECTRONICS									*
* 																					*
* - Driver:   			QuaXcopter "QXC" Communications  							*
* 																					*
* - Compiler:           Code Composer Studio (Licensed)								*
* - Supported devices:  All Stellaris Family Cortex-M3.								*
* - AppNote:			Message Structure (Server-Client)																	*
*																					*
* 	Created by: 		Juan Francisco Gomez Ruiz									*
*   Date Created: 		14 - JUN - 2012												*
*   Contac				pacoduz@gmail.com											*
* 																					*
* 	Description: 		Handle Status, Sensor Information & Debug Messages 			*	
* 						To be transmited by UART to Wifly							*						
*   Device supported and tested: - LM3S5749 - LM3S3748 -							*
* 																				2012*
*************************************************************************************

*************************************************************************************
* 	Revision History:
*
*   Date          CP#           Author
*   MM-DD-YY      XXXXX:1       Initials   Description of change
*   -----------   -----------   --------   ------------------------------------
*    06-14-12      	1.0             JFGR	Initial version. 
* 	 06-20-12		1.1				JFGR	Supports Message Classification in 2 Ports
* 	 06-25-12		1.2				JFGR	Supports Message Display in 2 ports "Theory - have to test both ports same time" 			

*************************************************************************************
*
* File/
*	
* 
*/
	#include "lm3s5749.h"
	#include "inc/hw_ints.h"
	#include "inc/hw_memmap.h"
	#include "inc/hw_sysctl.h"
	#include "inc/hw_types.h"
	#include "driverlib/rom.h"
	#include "driverlib/uart.h"
	#include "stdio.h"
	#include "stdlib.h"
	#include "Imu9DOFDriver.h"
	#include "DigitalFilters.h"
	#include "Dcm.h"
	#include "Compass.h"
	#include "ControladoresPID.h"
	#include "DS16XXDriver.h"
	#include "ExpansionBoardComm.h"
	#define   GLOBAL_Q	16
	#include "IQmath/IQmathLib.h"

/******************************************************************************
*							 MACRO DEFINITIONS
******************************************************************************/	
	#define	AHRS_GUI_TI				0
	#define AHRS_GUI_QXC			1
	#define TERMINAL				2
	#define QXC_WIFI				3
	#define DATA_STRUCTURE			(QXC_WIFI)
	
	#define RAD_TO_DEGREES			(57.29577)
	#define DEGREES_TO_RAD			(0.0174533125)
	
	#if DATA_STRUCTURE == TERMINAL
	#define ROLL_LINE				0
	#define PITCH_LINE				1
	#define YAW_LINE				2
	#define TEMP_LINE				3
	#define PTEMP_LINE				4
	#define BATTERY_LINE			5
	#define HEIGHT_LINE				6
	#define THRUST_LINE				7
	#define COMMAND_LINE			8
	#define SET_ROLL_LINE			9
	#define SET_PITCH_LINE			10
	#define SET_YAW_LINE			11
	#define ROLL_ERROR_LINE			12
	#define ROLL_PID_LINE			13
	#define PITCH_ERROR_LINE		14
	#define PITCH_PID_LINE			15
	#define YAW_ERROR_LINE			16
	#define YAW_PID_LINE			17
	
	#define DATA_START				14
	#define MAX_DISPLAY				((YAW_PID_LINE+1)*CARACTERS_LENGTH)
	#define NUMBER_LINES			18
	#define CARACTERS_LENGTH		30
	#endif
	
	#if DATA_STRUCTURE == AHRS_GUI_TI
	#define ROLL_ANGLE				0
	#define PITCH_ANGLE				7
	#define YAW_ANGLE				15
	#define AHRS_DATA_LENGTH		30
	#endif	
	
	#if DATA_STRUCTURE == AHRS_GUI_QXC
	#define ROLL_ANGLE				0
	#define PITCH_ANGLE				7
	#define YAW_ANGLE				15
	#define SET_ROLL				23
	#define SET_PITCH				31
	#define SET_YAW					39
	#define SET_THRUST				47
	#define BOARD_TEMPERATURE		55
	#define PID_ROLL				63	
	#define PID_PITCH				71
	#define PID_YAW					79
	#define PID_HEIGHT				87
	#define MOTOR_X1				95
	#define MOTOR_X2				103
	#define MOTOR_Y1				111
	#define MOTOR_Y2				119
	#define COMMANDS				127
	#define AHRS_DATA_LENGTH		140
	#endif
	
	#if DATA_STRUCTURE == QXC_WIFI
	//MEssages type from Server -> Client
	#define CSCI					0
	#define CSIR					1
	//Messages type from Client -> Server
	#define	QXST					0
	#define QXIN					1
	#define QXDB					2
	#define QXTI					3
	#define QXMI					4
	#define QXSP					5
	
	#define DATA_LENGTH				8
	
	#define ROLL_ANGLE				0
	#define PITCH_ANGLE				1
	#define YAW_ANGLE				2
	#define HEIGHT					3
	#define SET_ROLL				4
	#define SET_PITCH				5
	#define SET_YAW					6
	#define SET_HEIGHT				7			
	#define BOARD_TEMPERATURE		8
	#define BATTERY					9
	
	#define THRUST					10
	#define SET_THRUST				11
	#define MOTOR_X1				12
	#define MOTOR_X2				13
	#define MOTOR_Y1				14
	#define MOTOR_Y2				15
	
	#define CONTROL_TYPE			16
	
	#define CO_GAS					17
	#define O3_GAS					18
	#define HUMIDITY				19
	#define TEMPERATURE				20
	
	#define PID_ROLL				21	
	#define PID_PITCH				22
	#define PID_YAW					23
	#define PID_HEIGHT				24
	
	#define ROLL_ERROR				25
	#define PITCH_ERROR				26
	#define YAW_ERROR				27
	#define HEIGHT_ERROR			28
		
	#define MESSAGE_LENGTH			((28*DATA_LENGTH)+DATA_LENGTH+2)
	#endif
/******************************************************************************
*   Global Type Definitions
******************************************************************************/
	enum{
		STATE_FILL=0,
		STATE_DISPLAY,
		STATE_FINISH
	};
	
	enum{
		SECTION0=0,
		SECTION1,
		SECTION2,
		SECTION3,
		SECTION4,
		SECTION5,
		SECTION6,
		SECTION7,
		SECTION8,
		SECTION9,
		SECTION_MAX
	};
	
/******************************************************************************
*   Global Struct Definitions
******************************************************************************/
	typedef struct						//Structure to handle State Machin, that controls:
	{									// Writing to PC Terminal.			 
		unsigned char ActualState;
		unsigned char NextState;
	}DisplaySM;

/******************************************************************************
*   					PUBLIC VARIABLES DEFINITIONS
******************************************************************************/		
	// State Machine status for each port UART0 = A & UART1 = B		//
	extern DisplaySM DisplayStatusA;
	extern DisplaySM DisplayStatusB;
	
	// Variables to control Received Data from UART 0	//
	extern volatile char DataControlBufferA[30];
	extern volatile char *DataControlAPtr;
	extern char DataControlAEngage;
	extern char NewDataControlA;
	// Message change Flag for each Port	//
	extern volatile char ChangeMessageTypeA;
	
	// Variables to control Received Data from UART 1	//
	extern volatile char DataControlBufferB[30];
	extern volatile char *DataControlBPtr;
	extern char DataControlBEngage;
	extern char NewDataControlB;
	// Message change Flag for each Port	//
	extern volatile char ChangeMessageTypeB;
	
	
	extern volatile short HeightCM_t,ProcessorTemperature_t;
	extern _iq BatteryStatus_t;

/******************************************************************************
*   					PUBLIC FUNCTIONS DEFINITIONS
******************************************************************************/
	// State Machine to write data to a PC terminal	//
	void VfnDisplayDataIntoTerminalA (void);
	
	// State Machine to write data to a WiFly / Xbee Module	//
	void VfnDisplayDataIntoTerminalB (void);
	
	// Function thta reset all state machines //
	void VfnDisplayDataInitialization (volatile unsigned char Port);
	
	// Function that Manage all message received from the Server.	//
	char VfnMessageManager (volatile unsigned char Port, char *BufferPtr, signed char *SetRoll, signed char *SetPitch, signed char *SetYaw, signed short *SetHeight, char *Hold);
	
#endif /*QXCCOMMUNICATIONS_H_*/
